<?php


namespace app\admin\controller\nurse;


use app\common\controller\Adminbase;
use think\Db;

class Pzlevel extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[]=['is_del','=',0];
            $where[]=['type','=',2];
            $_list = Db::name('pz_level')
                ->page($page, $limit)
                ->where($where)
                ->order('level')
                ->select();
            $total = Db::name('pz_level') ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }
    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('pz_level')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('pz_level')->where('id', $id)->update([
                    'is_del'=>1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'name' => $data['name'],
                'level' => $data['level'],
                'img_url' => $data['img_url'],
                'count' => $data['count'],
                'type' => 2
            );
            $stmt = Db::name('pz_level')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'name' => $data['name'],
                'count' => $data['count'],
                'level' => $data['level'],
                'img_url' => $data['img_url'],
            );
            $stmt = Db::name('pz_level')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('pz_level')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function server_list()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            Db::name('pz_level_money')
                ->where(['pz_level_id' => $data['pz_level_id']])
                ->delete();
            foreach ($data['server_data'] as $k => $v) {
                Db::name('pz_level_money')->insertGetId([
                    'pz_level_id' => $data['pz_level_id'],
                    'server_id' => $k,
                    'money' => $v['money'],
                    'create_time' => time(),
                ]);
            }
            $this->success('操作成功！');
        }
        $server_list = Db::name('server')->where(['is_del' => 0])->order('sort desc')->select();
        foreach ($server_list as $k => $v) {
            $server_list[$k]['money'] = 0;
            $mcard_serverc = Db::name('pz_level_money')->where(['server_id' => $v['id'], 'pz_level_id' => $id])->find();
            if ($mcard_serverc) {
                $server_list[$k]['money'] = $mcard_serverc['money'];
            }
        }
        $this->assign([
            'pz_level_id' => $id,
            'server_list' => $server_list
        ]);
        return $this->fetch('server');
    }


}